package band.wukong.practice.java.core.pack.io;

import java.io.File;
import java.io.IOException;
import java.util.Date;

/**
 * As you see...
 *
 * @author wukong(wukonggg@139.com)
 */
public class FileSample {

    public static void main(String[] args) throws Exception {
//        System.out.println(desc(new File(".")));
//        System.out.println(desc(new File("./pom.xml")));
//        mkdir("d:\\QMDownload\\kakaka");
//        System.out.println(rm(new File("d:\\QMDownload")));
        System.out.println(listDir(new File("D:\\Lab\\ws-wukong\\wukong-practice-java\\src\\main\\java\\band\\wukong\\practice\\java\\corepackage")));
    }

    private static String descFile(File file) throws IOException {
        if (null == file || !file.exists()) {
            throw new IllegalArgumentException("传入参数为NULL");
        }

        StringBuilder sb = new StringBuilder();
        if (file.isFile()) {
            sb.append("It's a file\n");
        } else if (file.isDirectory()) {
            sb.append("It's a directory\n");
        }

        sb.append("getName: ").append(file.getName())
                .append("\ngetParent: ").append(file.getParent())
                .append("\nlength: ").append(file.length())
                .append("\nlastModified: ").append(file.lastModified())
                .append("\ncan read: ").append(file.canRead())
                .append("\ncan write: ").append(file.canWrite())
                .append("\ngetPath: ").append(file.getPath())
                .append("\ngetAbsolutePath: ").append(file.getAbsolutePath())
                .append("\ngetCanonicalPath").append(file.getCanonicalPath())
                .append("\nSystem.getProperty(\"user.dir\") = " + System.getProperty("user.dir"));

        return sb.toString();
    }

    private static void mkdir(String path) throws Exception {
        if (null == path || path.equals("")) {
            throw new IllegalArgumentException();
        }

        File dir = new File(path);
        System.out.println("dir.mkdir() = " + dir.mkdir());
    }

    /**
     * 删除文件或目录。如果代表目录，则删除当前目录及目录下的所有内容
     */
    private static boolean rm(File file) {
        if (null == file || !file.exists()) {
            throw new IllegalArgumentException();
        }

        //如果是文件，则删除并返回
        if (file.isFile()) {
            return file.delete();
        }

        //如果file代表目录，就删除目录下所有子目录和文件
        File[] files = file.listFiles();
        if(null != files && files.length > 0) {
            //递归删除当前目录下的所有子目录和文件
            for (File lin : files) {
                rm(lin);
            }
        }

        return file.delete();
    }


    /**
     * 查看目录信息
     *
     */
    private static String listDir(File path) {
        if (null == path || !path.exists() || !path.isDirectory()) {
            throw new IllegalArgumentException();
        }

        if (path.isFile()) {
            return "file：" + path.getName()
                    + " canRead: " + path.canRead()
                    + " lastModified: " + new Date(path.lastModified());
        }

        StringBuilder info = new StringBuilder();
        File[] files = path.listFiles();
        if (null != files && files.length > 0) {
            //打印当前目录下包含的所有子目录和文件的名字
            info.append("dir summary: ").append(path.getName()).append(" - { ");
            for (File file : files) {
                info.append(file.getName()).append(" | ");
            }
            info.append("}\n");

            //打印当前目录下包含的所有子目录和文件的详细信息
            for (File file : files) {
                if (file.isFile()) {
                    info.append(file.getName())
                            .append(" canRead: ").append(file.canRead())
                            .append(" lastModified: ").append(new Date(file.lastModified()))
                            .append("\n");
                } else {
                    info.append(listDir(file));
                }
            }
        }

        return info.toString();
    }

}
